<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:s="http://jboss.com/products/seam/taglib"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:a="http://richfaces.org/a4j"
    xmlns:cgf="http://www.centralgold.com.br/facelets"
    template="/layout/template.xhtml">

<ui:define name="body">

	<script type="text/javascript">
        function setCaretToEnd (e) {
            var control = $((e.target ? e.target : e.srcElement).id);
            if (control.createTextRange) {
                var range = control.createTextRange();
                range.collapse(false);
                range.select();
            }
            else if (control.setSelectionRange) {
                control.focus();
                var length = control.value.length;
                control.setSelectionRange(length, length);
            }
            control.selectionStart = control.selectionEnd = control.value.length;
        } 

    </script>
	
    <h:form id="alugueisForm">

        <rich:panel>
            <f:facet name="header">Registrar Aluguel</f:facet>
            
            <rich:tabPanel switchType="client">
            	<rich:tab label="Ativos">
            		<rich:dataTable id="tabelaAlugueis" 
						value="#{cadastrarAluguel.listaAlugueis}"
						rows="10"
						onRowMouseOver="this.style.backgroundColor='#6495ED'"
       					onRowMouseOut="this.style.backgroundColor='#FFFFCC'"
						var="_aluguel"
						style="width: 750px !important;">
						
						<a:queue ignoreDupResponses="true" requestDelay="500" />
						
						<f:facet name="header">
							<rich:columnGroup>
								<rich:column>
									<h:outputText value="" />
								</rich:column>
								<rich:column>
									<h:outputText value="Nº" />
								</rich:column>
								<rich:column>
									<h:outputText value="Automóvel" />
								</rich:column>
								<rich:column>
									<h:outputText value="Cliente" />
								</rich:column>
								<rich:column rendered="false">
									<h:outputText value="Data de Saída" />
								</rich:column>
								<rich:column>
									<h:outputText value="Data prevista p/ entrega" />
								</rich:column>
								<rich:column>
									<h:outputText value="CPF/CNPJ" />
								</rich:column>
								<rich:column>
									<h:outputText value="Imprimir" />
								</rich:column>
								<rich:column>
									<h:outputText value="" />
								</rich:column>
							</rich:columnGroup>
						</f:facet>	
						
						<rich:column>
							<f:facet name="header">
								<h:selectBooleanCheckbox value="#{cadastrarAluguel.selecionadoTodos}" >
									<a:support event="onchange" 
										action="#{cadastrarAluguel.selecionaTodos()}"
										reRender="tabelaAlugueis"/>
								</h:selectBooleanCheckbox>
							</f:facet>
							<h:selectBooleanCheckbox value="#{_aluguel.selecionado}" />				
						</rich:column>
						
						<rich:column filterMethod="#{cadastrarAluguel.filterIds}">
							<f:facet name="header">
							 <h:inputText value="#{cadastrarAluguel.filterValue}" id="input"
							 	style="width:20px;">
		                        <a:support event="onkeyup" reRender="tabelaAlugueis, scroller"
		                            ignoreDupResponses="true" requestDelay="500"
		                            oncomplete="setCaretToEnd(event);"/>
		                    </h:inputText>
							</f:facet>
							<h:graphicImage value="/img/tool_f2.png"
								title="Existe(m) solicitação(ões) de desconto não atendida(s)" 
								rendered="#{_aluguel.showIconeAlerta}"/>
							<h:outputText value="#{_aluguel.idAluguelFormatado}" />				
						</rich:column>
						
						<rich:column filterBy="#{_aluguel.automovel.marca}" filterEvent="onkeyup">
							<f:facet name="header"></f:facet>
							<h:outputText value="#{_aluguel.automovel.marca}/#{_aluguel.automovel.modelo} - #{_aluguel.automovel.cor}" />
							<a:queue ignoreDupResponses="true" requestDelay="500" />				
						</rich:column>
						
						<rich:column filterBy="#{_aluguel.cliente.nome}" filterEvent="onkeyup">
							<f:facet name="header"></f:facet>
							<h:outputText value="#{_aluguel.cliente.nome}" />
							<a:queue ignoreDupResponses="true" requestDelay="500" />				
						</rich:column>
						
						<rich:column filterBy="#{_aluguel.dataSaida}" filterEvent="onkeyup"
							rendered="false">
							<f:facet name="header"></f:facet>
							<h:outputText value="#{_aluguel.dataSaida}" >
								<s:convertDateTime pattern="dd/MM/yyyy - HH:mm"/>
							</h:outputText>				
							<a:queue ignoreDupResponses="true" requestDelay="500" />
						</rich:column>
						
						<rich:column filterBy="#{_aluguel.dataPrevistaEntrega}" filterEvent="onkeyup">
							<f:facet name="header"></f:facet>
							<h:outputText value="#{_aluguel.dataPrevistaEntrega}" >
								<s:convertDateTime pattern="dd/MM/yyyy - HH:mm"/>
							</h:outputText>
							<a:queue ignoreDupResponses="true" requestDelay="500" />				
						</rich:column>
						
						<rich:column filterBy="#{_aluguel.cliente.cpf}" filterEvent="onkeyup">
							<f:facet name="header"></f:facet>
							<h:outputText value="#{_aluguel.cliente.cpf}" 
								converter="conversorCPFCnpj"/>
							<a:queue ignoreDupResponses="true" requestDelay="500" />				
						</rich:column>
						
						<rich:column rendered="true">
							<f:facet name="header"></f:facet>
							<h:commandLink id="btnImprimirContrato"
								action="#{cadastrarAluguel.imprimirContrato(_aluguel)}" 
								target="#">
								<h:graphicImage value="/img/btn_contrato.png" title="IMPRIMIR CONTRATO 2" />
							</h:commandLink>
							<rich:spacer width="5px"/>
							<h:commandLink id="btnImprimirRecibo"
								action="#{cadastrarAluguel.imprimirRecibo(_aluguel)}" 
								target="#">
								<h:graphicImage value="/img/btn_recibo.png" title="IMPRIMIR RECIBO 2" />
							</h:commandLink>
							<rich:spacer width="5px"/>
							<h:commandLink id="btnImprimirAutorizacao"
								action="#{cadastrarAluguel.imprimirAutorizacao(_aluguel)}" 
								target="#">
								<h:graphicImage value="/img/btn_autorizacao.png" title="IMPRIMIR AUTORIZAÇÃO 2" />
							</h:commandLink>	
							<rich:spacer width="5px"/>
							<h:commandLink id="btnImprimirTermoAdesao"
								action="#{cadastrarAluguel.imprimirTermoProtecao(_aluguel)}" 
								target="#">
								<h:graphicImage value="/img/btn_adesao.png" title="IMPRIMIR TERMO DE ADESÃO 2" />
							</h:commandLink>
							<rich:spacer width="5px"/>
							<h:commandLink id="btnImprimirProrrogacao"
								action="#{cadastrarAluguel.imprimirTermoProrrogacao(_aluguel)}" 
								target="#">
								<h:graphicImage value="/img/btn_prorrogar.png" title="IMPRIMIR TERMO DE PRORROGAÇÃO 2" />
							</h:commandLink>
							<rich:spacer width="5px"/>	
							<ui:remove>
							<!-- 
							 -->
							<!-- 
							<h:commandLink id="btnImprimirRecibo"
								action="#{cadastrarAluguel.imprimirRecibo(_aluguel)}" 
								target="#">
								<h:graphicImage value="/img/btn_recibo.png" title="IMPRIMIR RECIBO 2" />
							</h:commandLink>
							<h:commandButton value="RECIBO"
								alt="IMPRIMIR RECIBO"
								title="IMPRIMIR RECIBO"
								image="/img/btn_recibo.png" 
								action="#{cadastrarAluguel.imprimirRecibo(_aluguel)}" />	
							 -->
							<!-- 
							<rich:spacer width="5px"/>	
							<h:commandLink id="btnImprimirAutorizacao"
								action="#{cadastrarAluguel.imprimirAutorizacao(_aluguel)}" 
								target="#">
								<h:graphicImage value="/img/btn_autorizacao.png" title="IMPRIMIR AUTORIZAÇÃO 2" />
							</h:commandLink>		
							<h:commandButton value="AUTORIZAÇÃO"
								alt="IMPRIMIR AUTORIZAÇÃO"
								title="IMPRIMIR AUTORIZAÇÃO"
								image="/img/btn_autorizacao.png" 
								action="#{cadastrarAluguel.imprimirAutorizacao(_aluguel)}" />
							 -->			
							<!-- 
							<rich:spacer width="5px"/>	
							<h:commandButton value="TERMO DE ADESÃO"
								alt="IMPRIMIR TERMO DE ADESÃO"
								title="IMPRIMIR TERMO DE ADESÃO"
								image="/img/btn_adesao.png" 
								action="#{cadastrarAluguel.imprimirTermoProtecao(_aluguel)}" />		
							 -->					
							<!-- 
							<h:commandLink id="btnImprimirTermoAdesao"
								action="#{cadastrarAluguel.imprimirTermoProtecao(_aluguel)}" 
								target="#">
								<h:graphicImage value="/img/btn_adesao.png" title="IMPRIMIR TERMO DE ADESÃO 2" />
							</h:commandLink>
							<rich:spacer width="5px"/>			
							<h:commandButton value="PRORROGAÇÃO"
								alt="IMPRIMIR TERMO DE PRORROGAÇÃO"
								title="IMPRIMIR TERMO DE PRORROGAÇÃO"
								image="/img/btn_prorrogar.png" 
								action="#{cadastrarAluguel.imprimirTermoProrrogacao(_aluguel)}" />
									
							<h:commandLink id="btnImprimirProrrogacao"
								action="#{cadastrarAluguel.imprimirTermoProrrogacao(_aluguel)}" 
								target="#">
								<h:graphicImage value="/img/btn_prorrogar.png" title="IMPRIMIR TERMO DE PRORROGAÇÃO 2" />
							</h:commandLink>	
							 -->			
						</ui:remove>						
						</rich:column>
						
						<rich:column rendered="#{identity.hasRole('CADASTRAR_ALUGUEL_GRAVAR')}">
							<f:facet name="header"></f:facet>
							<h:commandButton value="EDITAR" 
								image="/img/btn_editar.png"
								alt="EDITAR ALUGUEL"
								title="EDITAR ALUGUEL"
								action="#{cadastrarAluguel.editar(_aluguel,false)}" />
							<rich:spacer width="5px"/>
							<h:commandButton value="FINALIZAR" 
								image="/img/btn_finalizar.png" 
								action="#{cadastrarAluguel.editar(_aluguel,true)}"
								alt="FINALIZAR ALUGUEL"
								title="FINALIZAR ALUGUEL"
								rendered="#{_aluguel.dataRetorno == null}"/>
						</rich:column>
					</rich:dataTable>
					<rich:datascroller for="tabelaAlugueis" id="scroller" renderIfSinglePage="false"/>
            	</rich:tab>
            	
				<rich:tab label="Finalizados">
            		<rich:dataTable id="tabelaAlugueisFinalizados" 
						value="#{cadastrarAluguel.listaAlugueisFinalizados}"
						rows="10"
						onRowMouseOver="this.style.backgroundColor='#6495ED'"
       					onRowMouseOut="this.style.backgroundColor='#FFFFCC'"
						var="_aluguel">
						
						<f:facet name="header">
							<rich:columnGroup>
								<rich:column>
									<h:outputText value="" />
								</rich:column>
								<rich:column>
									<h:outputText value="Nº" />
								</rich:column>
								<rich:column>
									<h:outputText value="Automóvel" />
								</rich:column>
								<rich:column>
									<h:outputText value="Cliente" />
								</rich:column>
								<rich:column rendered="false">
									<h:outputText value="Data de Saída" />
								</rich:column>
								<rich:column>
									<h:outputText value="Data de retorno" />
								</rich:column>
								<rich:column>
									<h:outputText value="CPF/CNPJ" />
								</rich:column>
								<rich:column>
									<h:outputText value="Imprimir" />
								</rich:column>
								<rich:column>
									<h:outputText value="" />
								</rich:column>
							</rich:columnGroup>
						</f:facet>	
						
						<rich:column>
							<f:facet name="header">
								<h:selectBooleanCheckbox value="#{cadastrarAluguel.selecionadoTodos}" >
									<a:support event="onchange" 
										action="#{cadastrarAluguel.selecionaTodosFinalizados()}"
										reRender="tabelaAlugueisFinalizados"/>
								</h:selectBooleanCheckbox>
							</f:facet>
							<h:selectBooleanCheckbox value="#{_aluguel.selecionado}" />				
						</rich:column>
						
						<rich:column filterMethod="#{cadastrarAluguel.filterIdsFinalizados}">
							<f:facet name="header">
							 <h:inputText value="#{cadastrarAluguel.filterValueFin}" id="input"
							 	style="width:20px;">
		                        <a:support event="onkeyup" reRender="tabelaAlugueisFinalizados, scrollerFin"
		                            ignoreDupResponses="true" requestDelay="500"
		                            oncomplete="setCaretToEnd(event);"/>
		                    </h:inputText>
							</f:facet>
							<h:outputText value="#{_aluguel.idAluguel}" />				
						</rich:column>
						
						<rich:column filterBy="#{_aluguel.automovel.marca}" filterEvent="onkeyup">
							<f:facet name="header"></f:facet>
							<h:outputText value="#{_aluguel.automovel.marca}/#{_aluguel.automovel.modelo} - #{_aluguel.automovel.cor}" />
							<a:queue ignoreDupResponses="true" requestDelay="500" />				
						</rich:column>
						
						<rich:column filterBy="#{_aluguel.cliente.nome}" filterEvent="onkeyup">
							<f:facet name="header"></f:facet>
							<h:outputText value="#{_aluguel.cliente.nome}" />	
							<a:queue ignoreDupResponses="true" requestDelay="500" />			
						</rich:column>
						
						<rich:column filterBy="#{_aluguel.dataSaida}" filterEvent="onkeyup" rendered="false">
							<f:facet name="header"></f:facet>
							<h:outputText value="#{_aluguel.dataSaida}" >
								<s:convertDateTime pattern="dd/MM/yyyy - HH:mm"/>
							</h:outputText>				
							<a:queue ignoreDupResponses="true" requestDelay="500" />
						</rich:column>
						
						<rich:column filterBy="#{_aluguel.dataRetorno}" filterEvent="onkeyup">
							<f:facet name="header"></f:facet>
							<h:outputText value="#{_aluguel.dataRetorno}" >
								<s:convertDateTime pattern="dd/MM/yyyy - HH:mm"/>
							</h:outputText>				
							<a:queue ignoreDupResponses="true" requestDelay="500" />
						</rich:column>
						
						<rich:column filterBy="#{_aluguel.cliente.cpf}" filterEvent="onkeyup">
							<f:facet name="header"></f:facet>
							<h:outputText value="#{_aluguel.cliente.cpf}" 
								converter="conversorCPFCnpj"/>				
							<a:queue ignoreDupResponses="true" requestDelay="500" />
						</rich:column>
						
						<rich:column rendered="true">
							<f:facet name="header"></f:facet>
							<h:commandButton value="CONTRATO"
								alt="IMPRIMIR CONTRATO"
								title="IMPRIMIR CONTRATO"
								image="/img/btn_contrato.png" 
								action="#{cadastrarAluguel.imprimirContrato(_aluguel)}" />
							<rich:spacer width="5px"/>						
							<h:commandButton value="RECIBO"
								alt="IMPRIMIR RECIBO"
								title="IMPRIMIR RECIBO"
								image="/img/btn_recibo.png" 
								action="#{cadastrarAluguel.imprimirRecibo(_aluguel)}" />	
							<rich:spacer width="5px"/>						
							<h:commandButton value="AUTORIZAÇÃO"
								alt="IMPRIMIR AUTORIZAÇÃO"
								title="IMPRIMIR AUTORIZAÇÃO"
								image="/img/btn_autorizacao.png" 
								action="#{cadastrarAluguel.imprimirAutorizacao(_aluguel)}" />
							<rich:spacer width="5px"/>						
							<h:commandButton value="TERMO DE ADESÃO"
								alt="IMPRIMIR TERMO DE ADESÃO"
								title="IMPRIMIR TERMO DE ADESÃO"
								image="/img/btn_adesao.png" 
								action="#{cadastrarAluguel.imprimirTermoProtecao(_aluguel)}" />	
							<h:commandButton value="PRORROGAÇÃO"
								rendered="false"
								alt="IMPRIMIR TERMO DE PRORROGAÇÃO"
								title="IMPRIMIR TERMO DE PRORROGAÇÃO"
								image="/img/btn_prorrogar.png" 
								action="#{cadastrarAluguel.imprimirTermoProrrogacao(_aluguel)}" />		
						</rich:column>
						
						<rich:column rendered="#{identity.hasRole('CADASTRAR_ALUGUEL_GRAVAR')}">
							<f:facet name="header"></f:facet>
							<h:commandButton value="EDITAR" 
								image="/img/btn_editar.png"
								alt="EDITAR ALUGUEL"
								title="EDITAR ALUGUEL"
								action="#{cadastrarAluguel.editar(_aluguel,false)}" />
							<rich:spacer width="5px"/>
							<h:commandButton value="FINALIZADO" 
								image="/img/btn_finalizado.png" 
								disabled="true"
								alt="ALUGUEL FINALIZADO"
								title="ALUGUEL FINALIZADO"
								rendered="#{_aluguel.dataRetorno != null}"/>
						</rich:column>
					</rich:dataTable>
					<rich:datascroller for="tabelaAlugueisFinalizados" id="scrollerFin" renderIfSinglePage="false"/>
            	</rich:tab>
            </rich:tabPanel>
        </rich:panel>

        <div class="actionButtons">
            <h:commandButton id="novo" value="Novo" action="#{cadastrarAluguel.novo()}"
            	rendered="#{identity.hasRole('CADASTRAR_ALUGUEL_GRAVAR')}"/>
            <h:commandButton id="excluir" value="Excluir" action="#{cadastrarAluguel.excluir()}"
            	rendered="#{identity.hasRole('CADASTRAR_ALUGUEL_EXCLUIR')}"
            	onclick="javascript:if(!confirm('Tem certeza que deseja excluir?'))return false;"/>
        </div>

    </h:form>

 </ui:define>
</ui:composition>
